<?php
class p_chapter extends Model{
	public static function getTableName(){
		return 'p_chapter';
	}

	public function set($object){
		$this->video_id = !isset($object->video_id) ? '0' : $object->video_id;
		$this->in_time = !isset($object->in_time) ? '0' : $object->in_time;
		$this->out_time = !isset($object->out_time) ? '0' : $object->out_time;
		$this->ii_id = !isset($object->ii_id) ? '0' : $object->ii_id;
		$this->thumbnail_url = !isset($object->thumbnail_url) ? '' : $object->thumbnail_url;
		$this->source_url = !isset($object->source_url) ? '' : $object->source_url;
		$this->location = !isset($object->location) ? '' : $object->location;
		$this->highlighted_cast_roles = !isset($object->highlighted_cast_roles) ? '' : $object->highlighted_cast_roles;
		$this->casts = !isset($object->casts) ? '' : $object->casts;
		$this->available_tag_types = !isset($object->available_tag_types) ? '' : $object->available_tag_types;
		$this->tags = !isset($object->tags) ? '' : $object->tags;
		$this->infos = !isset($object->infos) ? '' : $object->infos;
		$this->status = !isset($object->status) ? '0' : $object->status;
		$this->created_at = !isset($object->created_at) ? '' : $object->created_at;
		$this->created_by = !isset($object->created_by) ? '' : $object->created_by;
		$this->modified_at = !isset($object->modified_at) ? '' : $object->modified_at;
		$this->modified_by = !isset($object->modified_by) ? '' : $object->modified_by;
	}

	public function getChapterList($video_id)
	{
        $sql = "
            select a.*,
                group_concat(title SEPARATOR '\n\t') as chapter_title,
                group_concat(description SEPARATOR '\n\t') as chapter_description
            from `p_chapter` a
            left join `p_chapter-language` b on a.id=b.chapter_id
			where video_id='{$video_id}' and status=".AC::STATUS_NEW."
            group by chapter_id
            order by a.in_time
            ";
		$ret = Model::$dbo['r']->findBySql(__class__, $sql);
		return $ret;
	}

    /**
     * get chapter id by video id
     * @param   $video_id   INT
     * @return  String
     */
    public function getChapterIDByVideoID($video_id)
    {
        if (empty($video_id))   return  NULL;
        $sql=   '
            SELECT
                GROUP_CONCAT(DISTINCT id)    AS  chpaterID
            FROM
                `p_chapter`
            WHERE
                video_id = "'. $video_id. '"
                AND status <> "'. AC::STATUS_DELETED. '"
            ';
        $result =   Model::$dbo['r']->findBySql(__class__, $sql);
        return  empty($result[0]->chpaterID)    ?   NULL    :   $result[0]->chpaterID;
    }

    /**
     * get proofread SQL
     * @param   $id Int
     * @return  Array
     */
    public function getProofread($id)
    {
        if (empty($id)) return  array();

        $sqlArr =   array(
            'chapter'                   => array('id = "'. $id. '"'),
            'chapter-language'          => array('chapter_id = "'. $id. '"'),
            'chapter-tvbcom_category'   => array('chapter_id = "'. $id. '"'),
        );

        $sqlArray   =   array();
        foreach ($sqlArr    as  $tableName  =>  $whereArray)
        {
            $sqlArray[$tableName]   =   Model::assembleGetProofreadSql($tableName, $whereArray);
		}
		return  $sqlArray;
    }
}
?>
